package com.example.chatgpt;

import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.chatgpt.bean.User;
import com.example.chatgpt.util.StatusBarUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

public class ChatGPTActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView bottomNavigationView;
    private NavController navController;
    private SharedPreferences dialogPreferences;
    private SharedPreferences.Editor edit;
    private MyApplication myApplication;
    private User userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_gptactivity);
        //设置状态栏字体为黑色
        StatusBarUtil.setStatusBarTextColorBlack(this);

        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        //设置条目背景填充颜色
        bottomNavigationView.setItemIconTintList(null);
        //设置导航栏条目的选中事件
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        //bottomNavigationView.setLabelVisibilityMode(NavigationBarView.LABEL_VISIBILITY_UNLABELED);

        //创建navController 通过navController来控制页面切换
        navController = Navigation.findNavController(this, R.id.navHostFragment);
        //获取sharedPreferences
        dialogPreferences = getSharedPreferences("dialog", Context.MODE_PRIVATE);
        edit = dialogPreferences.edit();

        myApplication = (MyApplication) getApplication();
        userInfo = myApplication.getUserInfo();
        Log.d("guaimiao", userInfo.getUser());
        reload();
    }

    //重新加载提醒框
    private void reload() {
        int flag = dialogPreferences.getInt("flag", 0);
        if (flag == 0) {
            MyAlertDialog();
        } else {
            return;
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menuHome:
                navController.popBackStack();
                navController.navigate(R.id.homeFragment);
                break;
            case R.id.menuPainting:
                navController.popBackStack();
                navController.navigate(R.id.paintingFragment);
                break;
            case R.id.menuMe:
                navController.popBackStack();
                navController.navigate(R.id.meFragment);
                break;
        }
        return true;
    }

    private void MyAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("使用须知");
        builder.setIcon(R.mipmap.notify);
        builder.setMessage("1 第一次使用时必须从上方钥匙进入设置你的api-key或者是绘画token，设置成功之后以后直接使用即可\n" +
                "2 如需更换api-key或者token点击钥匙进入更换即可\n" +
                "3 点击聊天图标即可进入聊天记录页面，点击查询按钮可以查询某一天的记录\n" +
                "4 聊天页面的清除图标不会清除数据库的数据而聊天记录页面的图标会清除数据库的记录，请谨慎操作\n" +
                "5 我的界面的两个参数设置无需点击回车，设置好后侧滑返回聊天界面后自动生效，温度是小数，最大令牌数是整数");
        builder.setPositiveButton("关闭", (dialogInterface, i) -> {
            dialogInterface.dismiss();
        });
        builder.setNegativeButton("不再提示", (dialogInterface, i) -> {
            edit.putInt("flag", 1);
            edit.commit();
            dialogInterface.dismiss();
        });
        builder.create().show();

    }
}